using System.Web.UI;
using Quiki.ExpressionParsers;

namespace Quiki.ElementFormatters
{
    public class ExternalLinkElementFormatter : LambdaElementFormatter
    {
        public ExternalLinkElementFormatter () : base (WikiTextElementType.ExternalLink, CustomBeginAction, null)
        {
        }

        private static void CustomBeginAction (WikiTextElement element, HtmlFormatterContext context)
        {
            string url = (string)element.Properties["url"];

            context.Writer.AddAttribute (HtmlTextWriterAttribute.Href, url.ToString ());
            context.Writer.RenderBeginTag (HtmlTextWriterTag.A);
            if (element.Properties.ContainsKey("text"))
                context.Writer.Write (element.Properties["text"]);
            else
                context.Writer.Write (url);
            context.Writer.RenderEndTag ();
        }
    }
}